require.config({
	paths:{
		'jquery' : 'jquery-1.8.3.min'
	}
});

require(['jquery','cookie'],function ($,cookie) {
	$('.add').click(function () {
		//获取点击的该商品信息
		var
			iId = $(this).data('id'),
			sUrl = $(this).data('url'),
			sName = $(this).data('name'),
			iPrice = $(this).data('price');

		var oGoods = {
			"id"    : iId,
			"url"   : sUrl,
			"name"  : sName,
			"price" : iPrice,
			"num"   : 1
		};

		//获取cookie
		var aGoods = cookie.getCookie('shopCar');
		//如果是第一次点击，则cookie中未空，获取不到数据，所以返回值为undefined,
		if(aGoods == undefined){
			aGoods = [];
		}else{
			aGoods = JSON.parse(cookie.getCookie('shopCar'));
		}

		var bBtn = false; //假设当前商品没有被添加过
		for(var i = 0; i < aGoods.length; i++){
			//如果遍历的商品ID等于即将添加的商品ID，说明该商品已经添加过
			if(aGoods[i].id == iId){
				bBtn = true;
				aGoods[i].num ++;
				break;
			}
		}
		if(!bBtn){
			aGoods.push(oGoods);
		}

		//将商品信息写入cookie
		cookie.addCookie('shopCar', JSON.stringify(aGoods), 7);

	});
})